package com.ccp.dev.qbdms.service;

import com.ccp.dev.core.basic.base.BaseService;
import com.ccp.dev.core.basic.util.ContextUtil;
import com.ccp.dev.core.basic.web.query.QueryFilter;
import com.ccp.dev.core.poi.excel.Excel;
import com.ccp.dev.core.util.StringUtil;
import com.ccp.dev.qbdms.dao.WCustomColumnsDao;
import com.ccp.dev.qbdms.model.WCustomColumns;
import com.ccp.dev.qbdms.util.ProjectUUIDUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * w_custom_columns 信息操作服务类
 *
 * @author y
 * @date 2020-05-08 10:14:12
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class WCustomColumnsService extends BaseService<WCustomColumns> {

    @Resource
    private WCustomColumnsDao dao;

    public HSSFWorkbook exportExcel(Excel excel,QueryFilter queryFilter){
        List<WCustomColumns> dataList = this.dao.queryAll(queryFilter);
        int startRow = 1;
        //给每一个单元格赋值
        for (WCustomColumns data : dataList) {
            excel.cell(startRow, 0).value(data.getId());
            //excel.cell(startRow, 1).value(data.getMc());
            //可在赋值时，根据需求进行格式化
            //excel.cell(startRow, 2).value(DateFormatUtil.formatDate(data.getRq()));
            //excel.cell(startRow, 3).value(data.getSj());
            //excel.cell(startRow, 4).value(data.getTenantId());
            startRow++;
        }
        return excel.getWorkBook();
    }

    /**
     * 保存更新数据
     * @param wCustomColumns  实体对象
     */
    public void save(WCustomColumns wCustomColumns){
        String id = wCustomColumns.getId();
        Long isDefault = wCustomColumns.getIsDefault();
        Map<String,Object> map = new HashMap<>(4);
        if(StringUtil.isEmpty(id)){
            if(WCustomColumns.ISDEFAULT_YES.equals(isDefault)){
                map.put("isDefault",WCustomColumns.ISDEFAULT_NO);
                map.put("creatUser", ContextUtil.getCurrentUserId());
                map.put("uri", wCustomColumns.getUri());
                dao.updateIsDefault(map);
            }
            //新增操作
            //新增数据前生成随机Id
            wCustomColumns.setId(ProjectUUIDUtils.getUUIDFor32());
            wCustomColumns.setCreatUser(ContextUtil.getCurrentUserId());
            wCustomColumns.setCreatTime(new Date());
            dao.add(wCustomColumns);
        }else{
            if(WCustomColumns.ISDEFAULT_YES.equals(isDefault)){
                map.put("isDefault",WCustomColumns.ISDEFAULT_NO);
                map.put("creatUser", ContextUtil.getCurrentUserId());
                map.put("uri", wCustomColumns.getUri());
                map.put("nid", id);
                dao.updateIsDefault(map);
            }
            dao.update(wCustomColumns);
        }

    }

    /**
     * 设置为默认
     * @param id 主键
     */
    public void setDefault(String id){
        WCustomColumns wCustomColumns = dao.getById(id);
        Map<String,Object> map = new HashMap<>(4);
        map.put("isDefault",WCustomColumns.ISDEFAULT_NO);
        map.put("creatUser",ContextUtil.getCurrentUserId());
        map.put("uri",wCustomColumns.getUri());
        dao.updateIsDefault(map);
        map.put("id",id);
        map.put("isDefault",WCustomColumns.ISDEFAULT_YES);
        dao.updateIsDefault(map);
    }

    /**
     * 添加自定义列
     * @param request HttpServletRequest
     */
    public void addCustomCols(HttpServletRequest request){
        String uri = request.getRequestURI();
        String contextPath = request.getContextPath();
        String userId = ContextUtil.getCurrentUserId();
        QueryFilter queryFilter = new QueryFilter(request,false);
        if(uri.startsWith(contextPath)) {
            uri = uri.replace(contextPath, "");
        }
        queryFilter.addFilter("uri",uri);
        queryFilter.addFilter("creatUser",userId);
        queryFilter.addFilter("isDefault", WCustomColumns.ISDEFAULT_YES);
        List<WCustomColumns> wCustomColumnsList = queryList(queryFilter);
        if(!wCustomColumnsList.isEmpty()){
            request.setAttribute("cols",wCustomColumnsList.get(0).getFieldJson());
        }
    }
}
